<?php

class Common_Controller_Helper_Content {

    /**
     *
     * @var Application_Model_Content
     */
    protected $model = null;

    public function __construct() {
        $this->model = new Application_Model_Content();
    }

    public function getContent($typ = 0, $name = null, $boxName = null, $tag = '*', $where = null) {

        $boxName = isset(Application_Model_Content::$boxes[$boxName]) ? $boxName : Application_Model_Content::BX_NULL;
        $front = Zend_Controller_Front::getInstance();
        $rows = $this->model
                ->table
                ->fetchAll(
                'status=' . Application_Model_Content::S_PUBLISHED . ''
                . '' . ( $name == null ? '' . ($boxName == Application_Model_Content::BX_NULL ? '' : ' and boxName=\'' . $boxName . '\'') : '') . ''
                . ' ' . (!is_array($typ) ? 'and typ=' . $typ : 'and typ in(' . implode(',', $typ) . ')') . ''
                . ' and (boxTag like \'' . ($tag == '*' ? '%' : '' . str_replace('*','%',$tag) . '') . '%\' or boxTag like \'*%\' or boxTag like \'%['.$front->getRequest()->getParam('id','-').']%\'  )'.(strlen($tag)>1?' and boxTag not like \'*!\' and boxTag not like \'*!'. str_replace('*','%',$tag) .'\'':'').''
                . '' . ($name == null ? '' : ' and name= \'' . $name . '\'') . ($where == null ? '' : ' and ' . $where)
                        , 'position DESC'
        );

        return $rows->count() > 0 ? $rows->toArray() : null;
    }

}
